from aiogram import Router, types
from aiogram.fsm.state import StatesGroup, State
from aiogram.filters import Command, Text
from aiogram.utils.keyboard import InlineKeyboardBuilder
from services.check_bd_input import check_year, check_month, check_day
from messages import month_mess, reset_text
from services.save_birthday import save_birthday

router = Router()


class RemindBirthday(StatesGroup):
    input_bd_name = State()
    input_bd_year = State()
    input_bd_month = State()
    input_bd_day = State()


@router.message(Command("remind_birthday"))
async def start_bd_fsm(message, state):
    builder = InlineKeyboardBuilder()
    builder.add(types.InlineKeyboardButton(text=reset_text, callback_data="reset_bd"))
    await message.answer(
        text="Давайте добавим ваш <b>день рождения</b> в базу данных! \n"
        "⌨ Для начала укажите <b>как вас зовут</b>?:",
        reply_markup=builder.as_markup(),
    )
    await state.set_state(RemindBirthday.input_bd_name)


@router.message(RemindBirthday.input_bd_name)
async def set_name_fsm(message, state):
    await state.update_data(user_name=message.text)
    builder = InlineKeyboardBuilder()
    builder.add(types.InlineKeyboardButton(text=reset_text, callback_data="reset_bd"))
    await message.answer(
        text="👍 Спасибо!\n"
        "⌨ Теперь, введите ваш <b>год рождения</b>(к примеру, <em>1990</em>):",
        reply_markup=builder.as_markup(),
    )
    await state.set_state(RemindBirthday.input_bd_year)


@router.message(RemindBirthday.input_bd_year)
async def set_year_fsm(message, state):
    check_flag, info_mess = check_year(message.text)

    if check_flag:
        await state.update_data(year=int(message.text))
        builder = InlineKeyboardBuilder()
        builder.add(
            types.InlineKeyboardButton(text=reset_text, callback_data="reset_bd")
        )
        await message.answer(month_mess, reply_markup=builder.as_markup())
        await state.set_state(RemindBirthday.input_bd_month)

    else:
        await message.answer(info_mess)


@router.message(RemindBirthday.input_bd_month)
async def set_month_fsm(message, state):
    check_flag, info_mess = check_month(message.text)

    if check_flag:
        await state.update_data(month=int(message.text))
        builder = InlineKeyboardBuilder()
        builder.add(
            types.InlineKeyboardButton(text=reset_text, callback_data="reset_bd")
        )
        await message.answer(
            text="👍 Спасибо.\n"
            "⌨ Теперь, пожалуйста, введите ваш <b>день рождения</b>:",
            reply_markup=builder.as_markup(),
        )
        await state.set_state(RemindBirthday.input_bd_day)
    else:
        await message.answer(info_mess)


@router.message(RemindBirthday.input_bd_day)
async def set_day_fsm(message, state):
    data = await state.get_data()

    check_flag, info_mess = check_day(message.text, data["year"], data["month"])

    if check_flag:
        await state.update_data(day=int(message.text))
        data = await state.get_data()
        await save_birthday(data)
        await message.answer(
            text="🏆 <b>Спасибо. Мы сохранили все необходимые данные!</b>"
        )
        await state.clear()
    else:
        await message.answer(info_mess)


@router.callback_query(Text("reset_bd"))
async def set_reset_fsm(callback, state):
    await state.clear()
    await callback.message.answer(text="❗ Вы отменили ввод данных!")
    await callback.answer()
